<?php

namespace Biz\model\store;

use Doraemon\pockets\datebase\BaseDbModel;

class StoreUserConsult extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'store_user_consult';

    const CONSULT_STATUS_NORMAL = 0;//默认（未联系）
    const CONSULT_STATUS_TEL = 1;//已联系

    const SOURCE_STORE_HOME = 1;//门店主页
    const SOURCE_STORE_ACTIVITY = 2;//门店活动
    const SOURCE_WIKI_DETAIL = 3;//商品详情

    const SOURCE_MAP = [
        self::SOURCE_STORE_HOME => '门店主页',
        self::SOURCE_STORE_ACTIVITY => '门店活动',
        self::SOURCE_WIKI_DETAIL => '商品详情'
    ];

    const  ACT_SOURCE_MAP = [
         'StoreHomePage' => self::SOURCE_STORE_HOME,
         'StoreServices' => self::SOURCE_STORE_ACTIVITY,
         'WikiInfoDetail'=> self::SOURCE_WIKI_DETAIL
    ];

    public static function getListByWhere($where, $field = '*')
    {
        return self::select($field, $where);
    }

    public static function add($data)
    {
        if (!isset($data['addtime'])) {
            $data['addtime'] = date('Y-m-d H:i:s');
        }
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $result = self::insert($data)->rowCount();
        if ($result) {
            return self::id();
        }

        return false;
    }

    public static function updateData($data, $where)
    {
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return true;
    }

    public static function getInfo($id)
    {
        return self::get('*', ['id' => $id]);
    }

    public static function getCount($where)
    {
        return self::count($where);
    }

    public static function getByWhere($where)
    {
        return self::get('*', $where);
    }

    /**
     * 获取一行
     * @param $where
     * @param string $field
     * @return array|null
     */
    public static function first($where, $field = '*')
    {
        $rs = self::get($field, $where);
        if (!$rs) {
            return null;
        }

        return $rs;
    }


}